<template>
    <div style="height: 95%;width: 100%;display:flex;justify-content:center;padding-top: 20px">
        <!--弹框DIV 设置弹框的大小的地方-->
        <div style="background: #fff;width: 700px;height: 600px;box-shadow: #D3DCE6 0px 0px 20px;">
            <div style="height:520px;border: #1C84C6;margin-top: 25px" class="scrollbar">
                <div style="height: 30px;">
                    <!--标题-->
                    <span style="font-size: 18px;margin-left: 25px;">
                        新建交付记录
                    </span>
                    <!--返回列表-->
                        <span style="margin-left: 500px;"  @click="tzdz">
                            <i slot="suffix" style="font-size: 30px;color: #D3DCE6" class="el-icon-close"></i>
                        </span>
                </div>
                <!--height:91%;表单在弹框显示多长-->
                <el-scrollbar style="height:91%;padding-top: 20px;">
                    <div class="shugan"></div>
                    <span style="margin-left: 10px;font-size: 14px;">基本信息</span>
                    <br/>
                    <br/>
                    <!--表单-->
                    <el-form style="margin-top:5px;margin-left: 30px;" size="mini" :rules="rules" :model="jllist" ref="jllist">
                        <el-row>
                            <el-col :span="10">
                                <el-form-item style="width:250px;" prop="clientId">
                                    <label slot="label" style="font-size: 13px">客户：</label><br/>
                                    <!--客户名称-->
                                    <el-popover
                                            style="width: 700px; height: 450px;"
                                            placement="right"
                                            trigger="click"
                                            v-model="kehu">
                                        <span style="font-size: 16px;">关联客户模块</span>
                                        <br/>
                                        <el-input @keyup.enter.native="cx" v-model="kehuinput" style="width: 210px; margin-left: 230px; margin-bottom: 10px;">
                                            <i slot="suffix" class="el-input__icon el-icon-search" @click="cx"></i>
                                        </el-input>
                                        <el-button type="primary" style="margin-left: 130px;">新 建</el-button>
                                        <el-table
                                                highlight-current-row
                                                border
                                                ref="kehulist"
                                                :data="kehulist">
                                            <el-table-column type="selection" width="50" align="center" property="clientId"></el-table-column>
                                            <el-table-column width="150" property="clientName" label="客户名称"></el-table-column>
                                            <el-table-column width="100" property="clientRank" label="级别"></el-table-column>
                                            <el-table-column width="150" property="clientMobile" label="手机号码"></el-table-column>
                                            <el-table-column property="clientSite" label="地址"></el-table-column>
                                        </el-table>
                                        <el-pagination
                                                style="margin-left: 350px;"
                                                layout="prev, pager, next"
                                                :page-size="pageSize"
                                                :current-page.sync="currPage"
                                                @current-change="handleCurrentChange"
                                                :total="totalSize">
                                        </el-pagination>
                                        <div style="text-align: center;width: 100%" class="dialog-footer">
                                            <el-button type="primary" @click="xzkehu">保 存</el-button>
                                            <el-button type="primary" @click="kehu = false">取 消</el-button>
                                        </div>
                                        <el-input v-if="this.xg==0" v-model="jllist.clientId.clientName" placeholder="+添加" slot="reference" :readonly='true' style="width: 250px;"></el-input>
                                    </el-popover>
                                    <el-input v-if="this.xg==1" :disabled="true" v-model="jllist.clientId.clientName" placeholder="+添加" :readonly='true' style="width: 250px;"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item style="width:250px" prop="deliveryId">
                                    <label slot="label" style="font-size: 13px">交付计划：</label><br/>
                                    <el-popover
                                            style="width: 700px; height: 450px;"
                                            placement="right"
                                            trigger="click"
                                            v-model="jihua">
                                        <span style="font-size: 16px;">关联计划模块</span>
                                        <br/>
                                        <el-input @keyup.enter.native="cx2" v-model="jihuainput" style="width: 210px; margin-left: 230px; margin-bottom: 10px;">
                                            <i slot="suffix" class="el-input__icon el-icon-search" @click="cx2"></i>
                                        </el-input>
                                        <el-button type="primary" style="margin-left: 130px;">新 建</el-button>
                                        <el-table
                                                highlight-current-row
                                                border
                                                ref="jihualist"
                                                :data="jihualist">
                                            <el-table-column type="selection" width="50" align="center" property="deliveryId"></el-table-column>
                                            <el-table-column width="150" property="deliveryTheme" label="主题"></el-table-column>
                                            <el-table-column width="200" property="orderId.orderTheme" label="订单"></el-table-column>
                                            <el-table-column width="100" property="deliveryMoney" label="总金额"></el-table-column>
                                            <el-table-column width="150" property="deliveryIssue" label="期次"></el-table-column>
                                        </el-table>
                                        <el-pagination
                                                style="margin-left: 350px;"
                                                layout="prev, pager, next"
                                                :page-size="pageSize2"
                                                :current-page.sync="currPage2"
                                                @current-change="handleCurrentChange2"
                                                :total="totalSize2">
                                        </el-pagination>
                                        <div style="text-align: center;width: 100%" class="dialog-footer">
                                            <el-button type="primary" @click="xzjihua">保 存</el-button>
                                            <el-button type="primary" @click="jihua = false">取 消</el-button>
                                        </div>
                                        <el-input v-if="this.xg==0" v-model="jllist.deliveryId.deliveryTheme" @focus="dakai" placeholder="+添加" slot="reference" :readonly='true' style="width: 250px;"></el-input>
                                    </el-popover>
                                    <el-input v-if="this.xg==1" :disabled="true" v-model="jllist.deliveryId.deliveryTheme" placeholder="+添加" :readonly='true' style="width: 250px;"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="10">
                                <el-form-item style="width:250px;">
                                    <label slot="label" style="font-size: 13px">产品总金额：</label><br/>
                                    <el-input :disabled="true" v-model="jllist.deliverylogNumber"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item style="width:250px" prop="deliverylogTime">
                                    <label slot="label" style="font-size: 13px">交付日期：</label><br/>
                                    <el-date-picker
                                            v-model="jllist.deliverylogTime"
                                            type="datetime"
                                            placeholder="选择日期时间"
                                            align="right"
                                            :picker-options="pickerOptions"
                                            style="width:250px">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="10">
                                <el-form-item style="width:250px" prop="deliverylogRecipient">
                                    <label slot="label" style="font-size: 13px">对方接收人：</label><br/>
                                    <el-input  v-model="jllist.deliverylogRecipient"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item style="width:250px" prop="deliverylogTime">
                                    <label slot="label" style="font-size: 13px">负责人：</label><br/>
                                    <el-input :disabled="true" v-model="jllist.deliverylogFzr"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="18">
                                <el-form-item>
                                    <label slot="label" style="font-size: 13px">备注：</label><br/>
                                    <el-input v-model="jllist.deliverylogRemark"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <br/>
                    <span style="margin-left:30px;font-size: 13px">产品</span>
                    <br/>
                        <el-table
                                :data="jllist.zdeliverylogdeliveriesByDeliverylogId"
                                style="width: 80%;margin-left: 10px;font-size:13px">
                            <el-table-column
                                    align="center"
                                    prop="prodetailId.productName"
                                    label="产品名称">
                            </el-table-column>
                            <el-table-column
                                    align="center"
                                    label="产品规格">
                                <template slot-scope="s">
                                        <span v-for="pro in s.row.prodetailId.qmodeldetails" :key="pro.prodetailId">
                                            {{pro.modetContent}}
                                        </span>
                                </template>
                            </el-table-column>
                            <el-table-column
                                    align="center"
                                    label="进价"
                                    prop="prodetailId.prodetaiJpri">
                            </el-table-column>
                            <el-table-column
                                    align="center"
                                    label="数量"
                                    prop="logdeliveryNumber">
                            </el-table-column>
                        </el-table>
                    <br/>
                    <div style="margin-left:440px;font-size: 12px;margin-top: 10px;">
                        <span>总金额：</span>
                        {{jllist.deliverylogNumber}}
                        <span>元</span>
                    </div>
                    <br/>
                </el-scrollbar>
                <!--确定按钮 设置了固定定位-->
                <div style="text-align: center;width: 80%;position: absolute;top:635px;left:230px;" class="dialog-footer">

                    <el-button  @click="tzdz">取 消</el-button>
                    &nbsp;&nbsp;
                    <el-button type="primary" @click="addbusiness('jllist')">确 定</el-button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import {formatTimeToStr} from '../router//format.js'
    export default {
        name: "ZxinzenJiaofuJilu",
        data () {
            var hueikuan = (rule, value, callback) => {
                if (this.jllist.clientId.clientName === null || this.jllist.clientId.clientName === '') {
                    callback(new Error('请先选择客户'));
                } else {
                    if (this.jllist.deliveryId.deliveryTheme=== '' || this.jllist.deliveryId.deliveryTheme=== null) {
                        callback(new Error('请选择交付计划'));
                    }else {
                        callback()
                    }
                }
            };
            var kehua = (rule, value, callback) => {
                if (value.clientName === null || value.clientName === '') {
                    callback(new Error('请选择客户'));
                }else {
                    callback()
                }
            };
            return {
                rules: {
                    clientId: [
                        {validator: kehua, required: true, trigger: ['blur','change']},
                    ],
                    deliveryId: [
                        {validator: hueikuan, required: true, trigger: ['blur','change']},
                    ],
                    deliverylogTime: [
                        {required: true, message: '请选择交付时间', trigger: 'change' },
                    ],
                    deliverylogRecipient: [
                        {required: true, message: '请输入产品接收人', trigger: 'change' },
                    ]
                },pickerOptions: {//开票日期快捷选择方式
                    shortcuts: [{
                        text: '今天',
                        onClick(picker) {
                            picker.$emit('pick', new Date());
                        }
                    }, {
                        text: '昨天',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24);
                            picker.$emit('pick', date);
                        }
                    }, {
                        text: '一周前',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', date);
                        }
                    }]
                },
                jllist:{
                    deliverylogId:0,
                    clientId:{
                        clientId:0,
                        clientName:'',
                    },
                    deliveryId:{
                        deliveryId:0,
                        deliveryTheme:''
                    },
                    deliverylogTime:'',
                    deliverylogRecipient:'',
                    deliverylogNumber:0,
                    deliverylogRemark:null,
                    deliverylogFzr:null,
                    zdeliverylogdeliveriesByDeliverylogId:[]
                }, //订单主表信息
                xg:0,//是否是修改
                kehu:false,//客户弹出框
                kehuinput:'',//客户弹框搜索框
                kehulist:[],//客户表格信息
                pageSize:5,//客户分页页大小
                currPage:1,//客户当前页码，默认为1
                totalSize:0,//客户总记录数
                jihua:false,//交付计划弹出框
                jihuainput:'',//交付计划弹框搜索框
                jihualist:[],//交付计划表格信息
                pageSize2:5,//交付计划分页页大小
                currPage2:1,//交付计划当前页码，默认为1
                totalSize2:0,//交付计划总记录数
                dbuspros:[],//交付计划产品集合
                ys:1,
                ydx:10,
                dz:''
            };
        } ,
        methods:{
            tzdz(){
                if(this.dz==='htzy'){
                    this.$router.push({
                        path:'/heton',
                        query: {ys:this.ys,ydx:this.ydx}
                    });
                }else {
                    this.$router.push({
                        path:'/jiaofujilu',
                        query: {ys:this.ys,ydx:this.ydx}
                    });
                }
            },
            /*客户当前页方法*/
            handleCurrentChange(val) {
                this.initData(val);
            },
            /*交付计划当前页方法*/
            handleCurrentChange2(val) {
                this.hetoninitData(val);
            },
            //客户弹框搜索方法
            cx(){
                this.initData(this.currPage);
            },
            //交付计划弹框搜索方法
            cx2(){
                this.jihuainitData(this.currPage2);
            },
            //确定按钮
            addbusiness(formName) {
                this.$refs[formName].validate(valid => {
                    this.print(valid);
                    window.console.log(valid)
                    if (valid) {
                        this.xzfapiao();
                    }else{
                        this.$message({
                            message: '验证不通过！',
                            type: 'warning'
                        });
                        return false;
                    }

                });
            },
            //点击合同输入框就查询合同信息
            dakai(){
                this.jihuainitData(this.currPage2);
            },
            //选择计划确定按钮
            xzjihua() {
                this.$refs.jihualist.selection.forEach((pro)=>{
                    this.jllist.deliverylogNumber=pro.deliveryMoney;
                    this.jllist.deliveryId=pro;
                });
                this.jihua=false;
                this.jihuainput='';
                this.jihuacpinitData();
            },
            //交付计划查询方法
            jihuainitData(dd){
                this.$axios.post("http://localhost:8088/jf/list_jh",this.qs.stringify(
                    {search3:'交付记录',search:this.jihuainput,search2:this.jllist.clientId.clientId, page:dd, size:this.pageSize2,}))
                    .then(v=>{
                        this.jihualist = v.data.rows;
                        this.totalSize2 = v.data.total;
                    })
                    .catch()
            },
            //选择客户确定按钮
            xzkehu() {
                this.$refs.kehulist.selection.forEach((pro)=>{
                    this.jllist.clientId=pro;
                });
                this.kehu=false;
                this.kehuinput='';
            },
            //客户查询方法
            initData(dd){
                this.$axios.post("http://localhost:8088/dindan/kehu_list",this.qs.stringify(
                    {search:this.kehuinput, page:dd, size:this.pageSize,}))
                    .then(v=>{
                        this.kehulist = v.data.rows;
                        this.totalSize = v.data.total;
                    })
                    .catch()
            },
            //根据交付计划ID查询交付计划详情所有产品信息
            jihuacpinitData(){
                this.$axios.post("http://localhost:8088/jf/list_jhxq",this.qs.stringify(
                    {search:this.jllist.deliveryId.deliveryId}))
                    .then(v=>{
                        v.data.forEach((xq,i)=>{
                            const jlxq = {};
                            this.$set(jlxq,'orderdetailId',0);
                            this.$set(jlxq,'logdeliveryNumber',xq.deliverydeliveryNumber);
                            this.$set(jlxq,'prodetailId',xq.prodetailId);
                            this.jllist.zdeliverylogdeliveriesByDeliverylogId.splice(i,0,jlxq);
                        })
                    })
                    .catch()
            },
            //根据记录ID查询记录详情所有产品信息
            jlxqinitData(dd){
                this.$axios.post("http://localhost:8088/jfjl/list_jlcp",this.qs.stringify(
                    {search:dd}))
                    .then(v=>{
                        this.jllist.zdeliverylogdeliveriesByDeliverylogId= v.data;
                    })
                    .catch()
            },
            //新增修改订单
            xzfapiao(){
                this.$axios.post("http://localhost:8088//jfjl/add_fapiao",this.jllist)
                    .then(()=>{
                        if(this.xg==1){
                            this.$message({
                                showClose: true,
                                type: 'success',
                                message: '修改成功!',
                            });
                        }else {
                            this.$message({
                                showClose: true,
                                type: 'success',
                                message: '新增成功!',
                            });
                        }
                        this.tzdz();
                    }).catch(()=>{
                    this.$message({
                        showClose: true,
                        message: '新增失败',
                        type: 'error'
                    });
                });
            },
        },
        created: function () {
            this.jllist.deliverylogFzr= JSON.parse(localStorage.getItem("user")).empName;
            this.initData(this.currPage);
            if(this.$route.query.key!=null){
                this.jllist=this.$route.query.key;
                this.jlxqinitData(this.jllist.deliverylogId);
                this.xg = 1;
            }
            if(this.$route.query.ys!=null){
                this.ys=this.$route.query.ys;
            }
            if(this.$route.query.ydx!=null){
                this.ydx=this.$route.query.ydx;
            }
            if(this.$route.query.dz!=null){
                this.dz=this.$route.query.dz;
            }
        },
        filters: {
            formatDate: function (time) {
                if (time != null && time != "") {
                    var date = new Date(time);
                    return formatTimeToStr(date, "yyyy-MM-dd");
                } else {
                    return "";
                }
            }
        }
    }
</script>

<style scoped>
    .shugan{
        border-left-color: rgb(70, 205, 207);
        border-left-width: 1.8px;
        height: 10px;
        border-left-style: solid;
        display: inline;
        font-size: 13px;
        margin-left: 25px;
    }

    /deep/ .el-scrollbar__wrap {
        overflow: scroll;
        width: 120%;
        height: 100%;
    }
</style>